/*
 * Table 메소드
 */

	function createTable() {
		var row = document.getElementById("rowCount").value;
		var col = document.getElementById("colCount").value;
		drawTable(row, col);
	}
	
	function drawTable(row, col) {
		var tableDIV = document.createElement("div");
		var newTable = document.createElement("table");
		newTable.setAttribute("id", "newTable");
		newTable.setAttribute("class", "newTable");
		newTable.style.border = "1px solid black";
		newTable.addEventListener("mousedown", selectCell, false); // 셀 선택 이벤트
	
		var tableBody = document.createElement("tbody");
		var tableCol, tableRowDiv;
		for (var i = 0; i < row; i++) {
			tableCol = document.createElement("tr");
			tableCol.style.border = "1px solid black";
			for (var j = 0; j < col; j++) {
				var cell = document.createElement("td");
				cell.style.border = "1px solid black";
	
				tableRowDiv = document.createElement("div");
				tableRowDiv.setAttribute("contenteditable", "true");
				tableRowDiv.style.width = "100px";
				tableRowDiv.style.height = "40px";
				tableRowDiv.style.padding = "7px";
	
				cell.appendChild(tableRowDiv);
				tableCol.appendChild(cell);
			}
			tableBody.appendChild(tableCol);
		}
	
		newTable.appendChild(tableBody);
		tableDIV.appendChild(newTable);
		document.getElementById("content").appendChild(tableDIV);
	}
	
	function removeTable() {
		var currentCell = $.find('td.highlight')[0];
		var $currentTable = currentCell.parentNode.parentNode.parentNode;
		$currentTable.remove();
	}
	
	function addTopRow() { // 위에 행 삽입
		var table = $('#newTable');
		var currentCell = table.find('td.highlight')[0];
	
		var topRowNum = currentCell.parentNode.rowIndex;
	
		var newRow = table[0].insertRow(topRowNum);
	
		for (var i = 0; i < table.find("tr")[0].cells.length; i++) {
			var newCell = newRow.insertCell(0);
			newCell.style.border = "1px solid black";
	
			tableRowDiv = document.createElement("div");
			tableRowDiv.setAttribute("contenteditable", "true");
			tableRowDiv.style.width = "100px";
			tableRowDiv.style.height = "40px";
			tableRowDiv.style.padding = "7px";
	
			newCell.appendChild(tableRowDiv);
		}
	}
	
	function addBottomRow() { // 아래에 행 삽입
		var table = $('#newTable');
		var currentCell = table.find('td.highlight')[0];
	
		console.dir(currentCell);
		var bottomRowNum = currentCell.parentNode.rowIndex + 1;
	
		var newRow = table[0].insertRow(bottomRowNum);
	
		for (var i = 0; i < table.find("tr")[0].cells.length; i++) {
			var newCell = newRow.insertCell(0);
			newCell.style.border = "1px solid black";
	
			tableRowDiv = document.createElement("div");
			tableRowDiv.setAttribute("contenteditable", "true");
			tableRowDiv.style.width = "100px";
			tableRowDiv.style.height = "40px";
			tableRowDiv.style.padding = "7px";
	
			newCell.appendChild(tableRowDiv);
		}
	}
	
	function addLeftCol() { // 왼쪽에 열 삽입
		var table = $('#newTable');
		var currentCell = table.find('td.highlight')[0];
	
		var leftColNum = currentCell.cellIndex;
		console.log("leftColNum = " + leftColNum);
	
		console.log("table row = " + table.find("tr").length);
	
		for (var i = 0; i < table.find("tr").length; i++) {
			var newCell = table.find("tr")[i].insertCell(leftColNum);
			console.dir(newCell);
			newCell.style.border = "1px solid black";
	
			tableRowDiv = document.createElement("div");
			tableRowDiv.setAttribute("contenteditable", "true");
			tableRowDiv.style.width = "100px";
			tableRowDiv.style.height = "40px";
			tableRowDiv.style.padding = "7px";
	
			newCell.appendChild(tableRowDiv);
		}
	}
	
	function addRightCol() { // 오른쪽에 열 삽입
		var table = $('#newTable');
		var currentCell = table.find('td.highlight')[0];
	
		var rightColNum = currentCell.cellIndex + 1;
		console.log("rightColNum = " + rightColNum);
	
		for (var i = 0; i < table.find("tr").length; i++) {
			var newCell = table.find("tr")[i].insertCell(rightColNum);
			console.dir(newCell);
			newCell.style.border = "1px solid black";
	
			tableRowDiv = document.createElement("div");
			tableRowDiv.setAttribute("contenteditable", "true");
			tableRowDiv.style.width = "100px";
			tableRowDiv.style.height = "40px";
			tableRowDiv.style.padding = "7px";
	
			newCell.appendChild(tableRowDiv);
		}
	}
	
	function deleteRow() { // 행 삭제
		var $table = $('#newTable');
		var currentCell = $table.find('td.highlight')[0];
	
		$(currentCell).parent().find('td').parent().remove();
	}
	
	function deleteCol() { // 열 삭제
		var $table = $('#newTable');
		var currentCell = $table.find('td.highlight')[0];
	
		var colNum = currentCell.cellIndex;
		console.dir(colNum);
		var $tdNodes = $table.find("tr").find("td:eq(" + colNum + ")");
		$tdNodes.remove();
	}
	
	function setBorderColor(color) { // 테두리 색상
		var newColor = color;
	
		$("td.highlight").css("border-top", "2px solid" + newColor);
		$("td.highlight").css("border-right", "2px solid" + newColor);
		$("td.highlight").css("border-left", "2px solid" + newColor);
		$("td.highlight").css("border-bottom", "2px solid" + newColor);
	}
	
	function setBackColor(color) { // 배경 색상
		var newColor = color;
		$("td.highlight").css("background-color", newColor);
	}
	
	function selectCell() { // 셀 선택
		var active = false;
		var table = $('#newTable');
		var startCol = 0;
		var startRow = 0;
		var endCol = 0;
		var endRow = 0;
		var start_cell;
	
		$(".content:not(.newTable)").mousedown(function() {
			$(".highlight").removeClass("highlight"); // 테이블 외부 선택시 highlight 해제
		});
	
		$(document).delegate("td", "mousedown", function() {
			active = true;
			$(".highlight").removeClass("highlight");
			$(this).addClass("highlight");
//			return false;
		}).delegate(
				"td",
				"mouseover",
				function() {
					if (active) {
						start_cell = table.find('td.highlight')[0];
	
						startCol = $.inArray(start_cell, $(start_cell).parent()
								.find('td'));
						startRow = $.inArray($(start_cell).parent()[0], table
								.find('tr'));
	
						endCol = $.inArray(this, $(this).parent().find('td'));
						endRow = $.inArray($(this).parent()[0], table.find('tr'));
	
						console.log("start = (" + startRow + "," + startCol + ")");
						console.log("end = (" + endRow + "," + endCol + ")");
	
						for (var j = startRow; j <= endRow; j++) {
							for (var i = startCol; i <= endCol; i++) {
								table.find('tr:eq(' + j + ')').find(
										'td:eq(' + i + ')').addClass("highlight");
							}
						}
						return false;
						// $(this).mouseout(function() { // 영역 벗어나면 highlight 해제
						// $(this).removeClass("highlight");
						// });
					}
				})
		/*
		 * .delegate("td", "mouseout", function() { // 영역 벗어나면 highlight 해제
		 * $(this).removeClass("highlight"); })
		 */
	
		.delegate("td", "mouseup", function() {
			active = false;
		});
	}
